package com.lill.common.component.util;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AESUtil {
	
	private final static Logger log = LoggerFactory.getLogger(AESUtil.class);

	////md5(新车界,16)
	public final static String secretKey="2EAC10469636D366";
	
	private static SecretKeySpec getSecretKey() {
		KeyGenerator keyGen=null;
		try {
			keyGen=KeyGenerator.getInstance("AES");
		} catch (NoSuchAlgorithmException e) {
			log.error(e.getMessage(), e);
		}
		keyGen.init(128, new SecureRandom(secretKey.getBytes()));
		SecretKey key=keyGen.generateKey();
		byte[] keyEncode = key.getEncoded();
		return new SecretKeySpec(keyEncode, "AES");
	}
	
	public static String encrypt16(String content) {
		String encryptContent="";
		try {
			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			byte[] byteContent = content.getBytes("utf-8");
			cipher.init(Cipher.ENCRYPT_MODE, getSecretKey());// 初始化为加密模式的密码器
			byte[] result = cipher.doFinal(byteContent);// 加密
			encryptContent=ParseSystemUtil.parseByte2HexStr(result);
		} catch (InvalidKeyException e) {
			log.error(e.getMessage(), e);
		} catch (NoSuchAlgorithmException e) {
			log.error(e.getMessage(), e);
		} catch (NoSuchPaddingException e) {
			log.error(e.getMessage(), e);
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage(), e);
		} catch (IllegalBlockSizeException e) {
			log.error(e.getMessage(), e);
		} catch (BadPaddingException e) {
			log.error(e.getMessage(), e);
		}
		return encryptContent;
	}
	
	public static String encrypt64(String content) {
		String encryptContent="";
		try {
			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			byte[] byteContent = content.getBytes("utf-8");
			cipher.init(Cipher.ENCRYPT_MODE, getSecretKey());// 初始化为加密模式的密码器
			byte[] result = cipher.doFinal(byteContent);// 加密
			encryptContent=Base64.encodeBase64String(result);
		} catch (InvalidKeyException e) {
			log.error(e.getMessage(), e);
		} catch (NoSuchAlgorithmException e) {
			log.error(e.getMessage(), e);
		} catch (NoSuchPaddingException e) {
			log.error(e.getMessage(), e);
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage(), e);
		} catch (IllegalBlockSizeException e) {
			log.error(e.getMessage(), e);
		} catch (BadPaddingException e) {
			log.error(e.getMessage(), e);
		}
		return encryptContent;
	}
	
	public static String decrypt16(String content) {
		String decryptContent="";
		try {
			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			cipher.init(Cipher.DECRYPT_MODE, getSecretKey());// 初始化为解密模式的密码器
			byte[] result = cipher.doFinal(ParseSystemUtil.parseHexStr2Byte(content));
			decryptContent=new String(result, "utf-8");
		} catch (InvalidKeyException e) {
			log.error(e.getMessage(), e);
		} catch (NoSuchAlgorithmException e) {
			log.error(e.getMessage(), e);
		} catch (NoSuchPaddingException e) {
			log.error(e.getMessage(), e);
		} catch (IllegalBlockSizeException e) {
			log.error(e.getMessage(), e);
		} catch (BadPaddingException e) {
			log.error(e.getMessage(), e);
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage(), e);
		}
		return decryptContent;
	}
	
	public static String decrypt64(String content) {
		String decryptContent="";
		try {
			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
			cipher.init(Cipher.DECRYPT_MODE, getSecretKey());// 初始化为解密模式的密码器
			byte[] result = cipher.doFinal(Base64.decodeBase64(content));
			decryptContent=new String(result, "utf-8");
		} catch (InvalidKeyException e) {
			log.error(e.getMessage(), e);
		} catch (NoSuchAlgorithmException e) {
			log.error(e.getMessage(), e);
		} catch (NoSuchPaddingException e) {
			log.error(e.getMessage(), e);
		} catch (IllegalBlockSizeException e) {
			log.error(e.getMessage(), e);
		} catch (BadPaddingException e) {
			log.error(e.getMessage(), e);
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage(), e);
		}
		return decryptContent;
	}
	
}
